﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.Video;
using GUI.Extensions;

namespace GUI.Controls.Video
{
    public partial class BoxVideo2 : System.Web.UI.UserControl
    {
        public string VideoKey { get; set; }
        public string Avatar { get; set; }
        public string VideoData { get; set; }
        private static readonly Dictionary<int, int> ZoneMapping = new Dictionary<int, int>
                                                                      {
                                                                        {10009,3},
                                                                        {10030,10},
                                                                        {10058,15},
                                                                        {10015,7},
                                                                        {10019,8},
                                                                        {10020,1},
                                                                        {10002,5},
                                                                        {10001,6},
                                                                        {10014,12},
                                                                        {10017,11},
                                                                        {10010,4},
                                                                        {10066,50}
                                                                      };

        private const int Width = 641;
        private const int Height = 426;

        protected void Page_Load(object sender, EventArgs e)
        {
            var objCat = RequestExtensions.Category;
            if (objCat != null)
            {
                var zoneId = objCat.ParentId > 0 ? objCat.ParentId : objCat.Id;
                var videoZoneId = 0;

                if (ZoneMapping.ContainsKey(zoneId))
                    ZoneMapping.TryGetValue(zoneId, out videoZoneId);
                var dt = VideoBO.GetVideoByMode(3, 1, videoZoneId);
                Visible = DataBind(dt);
                return;
            }
            Visible = false;
        }

        private bool DataBind(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new StringBuilder();

                var r = dt.Rows[0];
                var embedCode = PresentationUtils.Video.BuildEmbedCode(
                    r[SqlConstants.Video.KeyVideo].ToString(),
                    r[SqlConstants.Video.PName].ToString(),
                    Width, Height,
                    PresentationUtils.Photo.GetVideoAvatarSource(r[SqlConstants.Video.Avatar].ToString()),
                    title:r[SqlConstants.Video.Name].ToString());
                sb.AppendFormat("<li><p class=\"sprite icon\"><span></span><b id=\"video-title\">{0}</b></p><div class=\"clearfix\" id=\"video-embeb\">{1}</div></li>",
                    r[SqlConstants.Video.Name], embedCode);

                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    
                    ltrButtons.Text += string.Format(
                        "<a href=\"javascript:void(0)\" class=\"{0} sprite\" rel=\"{1}\"></a>",
                        i==0 
                        ? "active" 
                        : string.Empty,
                        dt.Rows[i][SqlConstants.Video.Id]);
                }
                VideoData = PresentationUtils.ObjectToJson(dt);

                ltrData.Text = sb.ToString();

                return true;
            }
            return false;
        }
    }
}